package com.example.screenplayer;

import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.nio.ByteBuffer;

public class ScreenSocketClient extends WebSocketClient {
  private static final String TAG = ScreenSocketClient.class.getSimpleName();
  private final SocketCallback mSocketCallback;

  public ScreenSocketClient(SocketCallback socketCallback, URI serverUri) {
    super(serverUri);
    mSocketCallback = socketCallback;
  }

  @Override
  public void onOpen(ServerHandshake serverHandshake) {
    Log.d(TAG, "onOpen");
  }

  @Override
  public void onMessage(String message) {}

  @Override
  public void onMessage(ByteBuffer bytes) {
    byte[] buf = new byte[bytes.remaining()];
    bytes.get(buf);
    if (mSocketCallback != null) {
      mSocketCallback.onReceiveData(buf);
    }
  }

  @Override
  public void onClose(int code, String reason, boolean remote) {
    Log.d(TAG, "onClose =" + reason);
  }

  @Override
  public void onError(Exception ex) {
    Log.d(TAG, "onError =" + ex.toString());
  }

  public interface SocketCallback {
    void onReceiveData(byte[] data);
  }
}
